home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume16 / hgrep < prev    next >
Encoding:
Internet Message Format  |  1988-11-10  |  7.1 KB

  1. Subject:  v16i072:  Highlighting grep filter
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: Jef Poskanzer <jef@helios.ee.lbl.gov>
  7. Posting-number: Volume 16, Issue 72
  8. Archive-name: hgrep
  9.  
  10. [  Jef's two-line summary is 100% on-the-mark.  In these days of huge
  11.    source postings to comp.sources.unix, let's not forget the little
  12.    tools and filters.  --r$  ]
  13.  
  14. Hgrep is a trivial, but cute, front-end for grep.  It takes the results of
  15. the grep and highlights the word that was searched for.
  16.  
  17. #! /bin/sh
  18. # This is a shell archive, meaning:
  19. # 1. Remove everything above the #! /bin/sh line.
  20. # 2. Save the resulting text in a file.
  21. # 3. Execute the file with /bin/sh (not csh) to create the files:
  22. #    README
  23. #    Makefile
  24. #    hgrep.c
  25. #    hgrep.1
  26. export PATH; PATH=/bin:$PATH
  27. echo shar: extracting "'README'" '(578 characters)'
  28. if test -f 'README'
  29. then
  30.     echo shar: will not over-write existing file "'README'"
  31. else
  32. sed 's/^X//' << \SHAR_EOF > 'README'
  33. X                      hgrep
  34. X             Distribution of 31oct87
  35. X
  36. XHgrep is a trivial, but cute, front-end for grep.  It takes the results of
  37. Xthe grep and highlights the word that was searched for.
  38. X
  39. XFiles in this distribution:
  40. X
  41. X    README        this
  42. X    Makefile        guess
  43. X    hgrep.c        source file
  44. X    hgrep.1        manual entry
  45. X
  46. XI've tested this stuff Suns and vaxen, but I'm sure bugs remain.
  47. XFeedback is welcome - send bug reports, enhancements, checks, money
  48. Xorders, etc. to the addresses below.
  49. X
  50. X    Jef Poskanzer
  51. X    jef@rtsg.ee.lbl.gov
  52. X    {ucbvax, lll-crg, sun!pacbell, apple, hplabs}!well!pokey
  53. SHAR_EOF
  54. if test 578 -ne "`wc -c < 'README'`"
  55. then
  56.     echo shar: error transmitting "'README'" '(should have been 578 characters)'
  57. fi
  58. fi # end of overwriting check
  59. echo shar: extracting "'Makefile'" '(319 characters)'
  60. if test -f 'Makefile'
  61. then
  62.     echo shar: will not over-write existing file "'Makefile'"
  63. else
  64. sed 's/^X//' << \SHAR_EOF > 'Makefile'
  65. X# Makefile for hgrep
  66. X
  67. Xall:        hgrep hgrep.cat
  68. X
  69. Xhgrep:        hgrep.c
  70. X    cc -O -s hgrep.c -ltermcap -o hgrep
  71. X
  72. Xhgrep.cat:    hgrep.1
  73. X    nroff -man hgrep.1 > hgrep.cat
  74. X
  75. Xclean:
  76. X    -rm -f hgrep hgrep.cat hgrep.shar
  77. X
  78. Xshar:        hgrep.shar
  79. X
  80. Xhgrep.shar:    README Makefile hgrep.c hgrep.1
  81. X    shar -v -c -p X README Makefile hgrep.c hgrep.1 > hgrep.shar
  82. SHAR_EOF
  83. if test 319 -ne "`wc -c < 'Makefile'`"
  84. then
  85.     echo shar: error transmitting "'Makefile'" '(should have been 319 characters)'
  86. fi
  87. fi # end of overwriting check
  88. echo shar: extracting "'hgrep.c'" '(3599 characters)'
  89. if test -f 'hgrep.c'
  90. then
  91.     echo shar: will not over-write existing file "'hgrep.c'"
  92. else
  93. sed 's/^X//' << \SHAR_EOF > 'hgrep.c'
  94. X/*
  95. X** hgrep - a front end to grep that highlights the string found
  96. X**
  97. X** version of 23oct88
  98. X**
  99. X** Copyright (C) 1988 by Jef Poskanzer.
  100. X**
  101. X** Permission to use, copy, modify, and distribute this software and its
  102. X** documentation for any purpose and without fee is hereby granted, provided
  103. X** that the above copyright notice appear in all copies and that both that
  104. X** copyright notice and this permission notice appear in supporting
  105. X** documentation.  This software is provided "as is" without express or
  106. X** implied warranty.
  107. X*/
  108. X
  109. X#include <stdio.h>
  110. X#include <string.h>
  111. X
  112. X#define TBUFSIZE 1024
  113. X#define BIGBUFSIZE 20000
  114. X
  115. Xextern char *getenv();
  116. Xextern char *tgetstr();
  117. Xvoid putch();
  118. X
  119. Xmain( argc, argv )
  120. Xint argc;
  121. Xchar *argv[];
  122. X    {
  123. X    char *term, *strptr, *soptr, *septr;
  124. X    int dumb = 0;
  125. X    int iflag = 0;
  126. X    char buf[TBUFSIZE];
  127. X    static char strbuf[TBUFSIZE];
  128. X    char bigbuf[BIGBUFSIZE];
  129. X    char *grepword = NULL;
  130. X    FILE *grepstream;
  131. X    int i, j, soon, somap[BIGBUFSIZE], grepwordlen;
  132. X
  133. X    /* Initialize termcap stuff. */
  134. X    if ( ! isatty( fileno( stdout ) ) )
  135. X    dumb = 1;
  136. X    else
  137. X    {
  138. X    term = getenv( "TERM" );
  139. X    if ( term == 0 )
  140. X        dumb = 1;
  141. X    else if ( tgetent( buf, term ) <= 0 )
  142. X        dumb = 1;
  143. X    else
  144. X        {
  145. X        strptr = strbuf;
  146. X        soptr = tgetstr( "so", &strptr );
  147. X        septr = tgetstr( "se", &strptr );
  148. X        if ( soptr == NULL || septr == NULL )
  149. X        dumb = 1;
  150. X        }
  151. X    }
  152. X
  153. X    /* Construct grep command. */
  154. X    strcpy( bigbuf, "grep" );
  155. X    for ( i = 1; i < argc; i++ )
  156. X    {
  157. X    strcat( bigbuf, " " );
  158. X    /* (Should do some kind of quoting here.) */
  159. X    strcat( bigbuf, argv[i] );
  160. X
  161. X    /* Check for -i flag. */
  162. X    if ( argv[i][0] == '-' && strchr( argv[i], 'i' ) != NULL )
  163. X        iflag = 1;
  164. X
  165. X    /* Save pointer to word we are grepping for - first non-flag arg. */
  166. X    if ( grepword == NULL && argv[i][0] != '-' )
  167. X        {
  168. X        grepword = argv[i];
  169. X        grepwordlen = strlen( grepword );
  170. X        }
  171. X    }
  172. X
  173. X    /* Spawn a grep. */
  174. X    grepstream = popen( bigbuf, "r" );
  175. X    if ( grepstream == NULL )
  176. X    {
  177. X    fprintf( stderr, "%s: can't spawn grep\n", argv[0] );
  178. X    exit( 1 );
  179. X    }
  180. X
  181. X    /* Now read and handle results of grep. */
  182. X    soon = 0;
  183. X    while ( fgets( bigbuf, BIGBUFSIZE, grepstream ) != NULL )
  184. X    {
  185. X    if ( dumb )
  186. X        fputs( bigbuf, stdout );
  187. X    else
  188. X        {
  189. X        /* Figure out what to highlight.  This is fairly inefficient,
  190. X        ** but since we are operating on the output of grep and not
  191. X        ** the input, it's ok. */
  192. X        for ( i = 0; bigbuf[i] != '\0'; i++ )
  193. X        somap[i] = 0;
  194. X        for ( i = 0; bigbuf[i] != '\0'; i++ )
  195. X        if ( iflag )
  196. X            {
  197. X            if ( cistrncmp( &(bigbuf[i]), grepword, grepwordlen ) == 0 )
  198. X            for ( j = i; j < i + grepwordlen; j++ )
  199. X                somap[j] = 1;
  200. X            }
  201. X        else
  202. X            {
  203. X            if ( strncmp( &(bigbuf[i]), grepword, grepwordlen ) == 0 )
  204. X            for ( j = i; j < i + grepwordlen; j++ )
  205. X                somap[j] = 1;
  206. X            }
  207. X
  208. X        /* And now do the highlighting. */
  209. X        for ( i = 0; bigbuf[i] != '\0'; i++ )
  210. X        {
  211. X        if ( somap[i] )
  212. X            {
  213. X            if ( ! soon )
  214. X            {
  215. X            tputs( soptr, 1, putch );
  216. X            soon = 1;
  217. X            }
  218. X            }
  219. X        else
  220. X            {
  221. X            if ( soon )
  222. X            {
  223. X            tputs( septr, 1, putch );
  224. X            soon = 0;
  225. X            }
  226. X            }
  227. X        putchar( bigbuf[i] );
  228. X        }
  229. X        }
  230. X    }
  231. X
  232. X    /* Bye. */
  233. X    if ( soon )
  234. X    tputs( septr, 1, putch );
  235. X    exit( pclose( grepstream ) );
  236. X    }
  237. X
  238. Xvoid
  239. Xputch( ch )
  240. Xchar ch;
  241. X    {
  242. X    putchar( ch );
  243. X    }
  244. X
  245. X
  246. X/* cistrncmp - case-insensitive version of strncmp */
  247. X
  248. X#include <ctype.h>
  249. X
  250. Xcistrncmp( s1, s2, n )
  251. Xchar *s1, *s2;
  252. Xint n;
  253. X    {
  254. X    while ( --n >= 0 && (isupper(*s1)?tolower(*s1):*s1) == (isupper(*s2)?tolower(*s2++):*s2++) )
  255. X    if ( *s1++ == '\0' )
  256. X        return 0;
  257. X    return n < 0 ? 0 : *s1 - *--s2;
  258. X    }
  259. SHAR_EOF
  260. if test 3599 -ne "`wc -c < 'hgrep.c'`"
  261. then
  262.     echo shar: error transmitting "'hgrep.c'" '(should have been 3599 characters)'
  263. fi
  264. fi # end of overwriting check
  265. echo shar: extracting "'hgrep.1'" '(348 characters)'
  266. if test -f 'hgrep.1'
  267. then
  268.     echo shar: will not over-write existing file "'hgrep.1'"
  269. else
  270. sed 's/^X//' << \SHAR_EOF > 'hgrep.1'
  271. X.TH HGREP 1 "23 October 1988"
  272. X.SH NAME
  273. Xhgrep - highlight results of a grep
  274. X.SH SYNOPSIS
  275. Xhgrep <grep args>
  276. X.SH DESCRIPTION
  277. X.I Hgrep
  278. Xis a trivial, but cute, front-end for
  279. X.I grep.
  280. XIt takes the results of the grep and highlights the word that was
  281. Xsearched for.
  282. X.SH "SEE ALSO"
  283. Xgrep(1)
  284. X.SH BUGS
  285. XMeta-characters are not handled.  Quoting is not handled.
  286. SHAR_EOF
  287. if test 348 -ne "`wc -c < 'hgrep.1'`"
  288. then
  289.     echo shar: error transmitting "'hgrep.1'" '(should have been 348 characters)'
  290. fi
  291. fi # end of overwriting check
  292. #    End of shell archive
  293. exit 0
  294.  
  295.